layui.use([ 'form', 'step'], function () {
    var $ = layui.$,
        form = layui.form,
        step = layui.step;

    step.render({
        elem: '#stepForm',
        filter: 'stepForm',
        width: '100%', //设置容器宽度
        stepWidth: '750px',
        height: '500px',
        stepItems: [{
            title: '填写请假信息'
        }, {
            title: '确认请假信息'
        }, {
            title: '完成'
        }]
    });


    form.on('submit(formStep)', function (data) {
        step.next('#stepForm');
        return false;
    });

    form.on('submit(formStep2)', function (data) {
        step.next('#stepForm');
        return false;
    });

    $('.pre').click(function () {
        step.pre('#stepForm');
    });

    $('.next').click(function () {
        step.next('#stepForm');
    });

    //获取请假数据
    $(function(){
        $.ajax({
            url:"../../api/askLeave.json",
            success:function(data){
            let myLeave=JSON.stringify(data.data);
            localStorage.setItem("myleave",myLeave);
        }
        });
        leaveInit();
    })

    //给表单赋值
    function leaveInit(){
        //json数据
        let Leave=JSON.parse(localStorage.getItem("myleave"));
        //循环数据
        one:
        for(let i=0;i<Leave.length;i++){
        //拿登录员工的id
        let tId=JSON.parse(localStorage.getItem("tId"));
        two:
        for(let j=0;j<tId.length;j++){
            let loginId=tId[0].id;
            // console.log(loginId);  //拿到当前员工的id
            if(loginId==Leave[i].id){
                //文本框显示的内容
                form.val("Leave",{
                    "id":Leave[i].id,
                    "name":Leave[i].userName,
                    "department":Leave[i].department
                });
                break one;
            }
        }
        }
    }

    //给下一步按钮绑定事件
    $("#next").on("click",function(){
        //拿登录员工的id
        let tId=JSON.parse(localStorage.getItem("tId"));
        let loginId=tId[0].id;
        //获取本地存储的数据
        let Leave=JSON.parse(localStorage.getItem("myleave"));
        //拿到表单元素的值
        let data = form.val('Leave');
        // console.log(data)
        for(let i=0;i<Leave.length;i++){
            if(loginId==Leave[i].id){
                Leave[i].askType=data.askType;
                Leave[i].content=data.content;
                Leave[i].status=1;
            }
        }
        //将最新的数据保存在本地
    localStorage.setItem("myleave",JSON.stringify(Leave));
    lookInit();
    });
    //给上一步添加事件
    $("#prve").on("click",function (){
        //拿登录员工的id
        let tId=JSON.parse(localStorage.getItem("tId"));
        let loginId=tId[0].id;
        //获取本地存储的数据
        let Leave=JSON.parse(localStorage.getItem("myleave"));
        //拿到表单元素的值
        let data = form.val('Leave');
        // console.log(data)
        for(let i=0;i<Leave.length;i++){
            if(loginId==Leave[i].id){
                Leave[i].status=0;
            }
        }
        //将最新的数据保存在本地
        localStorage.setItem("myleave",JSON.stringify(Leave));
        lookInit();
    })

    //给下一个表单赋值
    function lookInit(){
        //json数据
        let Leave=JSON.parse(localStorage.getItem("myleave"));
        //循环数据
        one:
        for(let i=0;i<Leave.length;i++){
        //拿登录员工的id
        let tId=JSON.parse(localStorage.getItem("tId"));
        two:
        for(let j=0;j<tId.length;j++){
            let loginId=tId[0].id;
            // console.log(loginId);  //拿到当前员工的id
            if(loginId==Leave[i].id){
                //文本框显示的内容
                console.log(Leave[i].userName)
                console.log($("#lookId").html())
                $("#lookId").html(Leave[i].id);
                $("#lookName").html(Leave[i].userName);
                $("#lookDepartment").html(Leave[i].department);
                $("#lookType").html(Leave[i].askType);
                $("#lookContent").html(Leave[i].content);
                break one;
            }
        }
        }
    }
})